//
//  YSHomeBannerView.m
//  Yueshi
//
//  Created by yueshi_tech on 15/7/17.
//  Copyright (c) 2015年 悦食中国. All rights reserved.
//  首页Banner

#import "YSHomeBannerView.h"
#import "SGFocusImageFrame.h"
#import "SGFocusImageItem.h"
#import "YSAdvert.h"
#import "YSBanner.h"
@interface YSHomeBannerView()<SGFocusImageFrameDelegate>
@property (nonatomic,strong)SGFocusImageFrame *bannerView;
@property (nonatomic,strong)NSMutableArray *itemArray;
@property (nonatomic,assign)HomeSubViewSourceType sourceType;
@end

@implementation YSHomeBannerView

-(instancetype)initWithAdvert:(YSCardBannerResult *)banner sourceType:(HomeSubViewSourceType)sourceType{
    self = [super init];
    if(self){
        [self inflateTopDataWithArray:banner.advert];
        self.sourceType = sourceType;
    }
    return self;
}

-(void)inflateTopDataWithArray:(NSArray *)bannerArray{
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    if(!bannerArray.count)return;
    NSMutableArray *itemArray = [NSMutableArray arrayWithCapacity:bannerArray.count+2];
    for (int i = 0; i < bannerArray.count; i++)
    {
        SGFocusImageItem *item = [[SGFocusImageItem alloc] initWithTitle:((YSAdvert *)bannerArray[i]).advertUrl image:((YSAdvert *)bannerArray[i]).advertImg tag:i];
        [itemArray addObject:item];
    }
    
    if(bannerArray.count>1){
        SGFocusImageItem *startItem = [[SGFocusImageItem alloc] initWithTitle:((YSAdvert *)[bannerArray lastObject]).advertUrl image:((YSAdvert *)[bannerArray lastObject]).advertImg tag:bannerArray.count-1];
        [itemArray insertObject:startItem atIndex:0];
        
        SGFocusImageItem *endItem = [[SGFocusImageItem alloc] initWithTitle:((YSAdvert *)[bannerArray lastObject]).advertUrl image:((YSAdvert *)[bannerArray firstObject]).advertImg tag:bannerArray.count-1];
        [itemArray addObject:endItem];
    }
   
    SGFocusImageFrame *bannerView = [[SGFocusImageFrame alloc] initWithFrame:CGRectMake(0, 0, SCREENW,SCREENW/1.5) delegate:self imageItems:itemArray isAuto:YES];
    [bannerView scrollToIndex:0];
    [self addSubview:bannerView];
}

-(void)foucusImageFrame:(SGFocusImageFrame *)imageFrame didSelectItem:(SGFocusImageItem *)item{
    NSLog(@"%s,%@",__FUNCTION__,item.title);
    if(_sourceType == HomeSourceType){
        [Utils dealUrlWithController:self.superview.viewController url:item.title fromWebView:NO];
    }else{
        [Utils dealUrlWithController:self.viewController url:item.title fromWebView:NO];
    }
}

@end
